package jp.co.ricoh.melody.models.entities;

import java.io.Serializable;
import java.util.Arrays;

import jp.co.ricoh.melody.utils.ConstantUtils;

/**
 * Copyright (C) 2016 RICOH Co.,LTD.
 * All rights reserved.
 */
public class AlarmData implements Serializable {

    public enum AMPM {
        AM, PM
    }

    private int mId;
    private String mLabel;
    private int mHours;
    private int mMinutes;
    private AMPM mAMPM;
    private boolean[] mDays = new boolean[7];
    private String mSoundTime;

    private boolean mIsRepeatEnabled;
    private boolean mIsEnabled;
    private SoundData mSoundData;
    private int mLastRepeatDay;

    private AlarmData() {
    }

    public AlarmData(int id, String label, int hours, int minutes, AMPM AMPM, boolean[] days, boolean isRepeatEnabled, boolean isEnabled, SoundData soundData, String soundTime) {
        validateTime(hours, minutes);
        mId = id;
        mLabel = label;
        mHours = hours;
        mMinutes = minutes;
        mAMPM = AMPM;
        mDays = Arrays.copyOf(days, days.length);
        mIsRepeatEnabled = isRepeatEnabled;
        mIsEnabled = isEnabled;
        mSoundData = soundData;
        mSoundTime = soundTime;
    }

    public AlarmData(String label, int hours, int minutes, AMPM AMPM, boolean[] days, boolean isRepeatEnabled, boolean isEnabled, SoundData soundData, String soundTime) {
        validateTime(hours, minutes);
        mLabel = label;
        mHours = hours;
        mMinutes = minutes;
        mAMPM = AMPM;
        mDays = Arrays.copyOf(days, days.length);
        mIsRepeatEnabled = isRepeatEnabled;
        mIsEnabled = isEnabled;
        mSoundData = soundData;
        mSoundTime = soundTime;
    }
    public AlarmData(String label, int hours, int minutes, AMPM AMPM, boolean[] days, boolean isRepeatEnabled, boolean isEnabled, SoundData soundData) {
        this(label,hours,minutes, AMPM,days,isRepeatEnabled,isEnabled,soundData,"10 sec");
    }
    private void validateTime(int hours, int minutes) {
        if (hours < ConstantUtils.HOURS_MIN || hours > ConstantUtils.HOURS_MAX) {
            throwException("Hours must less than 24 and larger -1");
        }
        if (minutes < ConstantUtils.MINUTES_MIN || minutes > ConstantUtils.MINUTES_MAX) {
            throwException("Minutes must less than 60 and larger -1");
        }
    }

    private void throwException(String msg) {
        try {
            throw new Exception(msg);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public SoundData getSoundData() {
        return mSoundData;
    }

    public AMPM getAMPM() {
        return mAMPM;
    }

    public int getId() {
        return mId;
    }

    public void setId(int id) {
        mId = id;
    }

    public String getLabel() {
        return mLabel;
    }

    public void setLabel(String label) {
        mLabel = label;
    }

    public int getHours() {
        return mHours;
    }

    public int getMinutes() {
        return mMinutes;
    }

    public boolean isRepeatEnabled() {
        return mIsRepeatEnabled;
    }

    public boolean isEnabled() {
        return mIsEnabled;
    }

    public void setEnabled(boolean enabled) {
        mIsEnabled = enabled;
    }

    public String getSoundTime() { return mSoundTime;}


    public boolean[] getDays() {
        if (mDays == null) {
            return null;
        }
        return Arrays.copyOf(mDays, mDays.length);
    }

    public int getLastRepeatDay() {
        return mLastRepeatDay;
    }

    public void setLastRepeatDay(int lastRepeatDay) {
        mLastRepeatDay = lastRepeatDay;
    }

    /**
     * compare
     *
     * @param other_alarm
     * @return
     */
    public boolean compare(AlarmData other_alarm) {
        if (!this.getLabel().contains(other_alarm.getLabel())) {
            return true;
        }

        if (this.getHours() != other_alarm.getHours() || this.getMinutes() != other_alarm.getMinutes() || this.getAMPM() != other_alarm.getAMPM()) {
            return true;
        }

        if (this.isRepeatEnabled() != other_alarm.isRepeatEnabled()) {
            return true;
        }

        for (int i = 0; i < this.getDays().length; i++) {
            if (this.getDays()[i] != (other_alarm.getDays()[i])) {
                return true;
            }
        }

        if (!this.getSoundData().getId().equals(other_alarm.getSoundData().getId())) {
            return true;
        }

        if (!this.getSoundTime().equals(other_alarm.getSoundTime())) {
            return true;
        }
        return false;
    }
}
